// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        skillPanel: cc.Node,
        skillIcon: cc.SpriteAtlas,
    },

    onClick(e) {
        let node = e.target;
        g_Slot.emit(g_Lister.add_skill, node.skill); //发送消息，将技能id加入到列表中
        this.node.destroy(); //销毁自身
        this.node.cbfunc(); //调用父节点的方法
    },

    // onLoad () {},

    start() {
        // let listkill = [8001, 8002, 8003, 8004, 8005, 8006, 8007];
        // let listResult = [];
        // let cnt = 3;
        // while (cnt--) {
        //     let idx = Math.floor(Math.random() * listkill.length);
        //     let icon = listkill[idx];
        //     listResult.push(icon); //将随机出来的图标加入到数组中
        //     listkill.splice(idx, 1); //将数组中已经使用的图标删除,1是长度，删除一个
        // }

        let listResult = g_Skill.randomSkill(3); //获取技能列表
        for (let child of this.skillPanel.children) {
            // child.on(cc.Node.EventType.TOUCH_END, () => {
            //     this.node.destroy();
            //     cc.find("Canvas/game").getComponent("game").gamestate = 1;
            // });
            let skill = listResult.shift(); //将数组中第一个元素取出来
            let obj = g_Skill.create(skill); //创建技能对象
            let sf = this.skillIcon.getSpriteFrame(skill); //获取图片
            child.getChildByName("skillicon").getComponent(cc.Sprite).spriteFrame = sf; //设置图片
            child.getChildByName("skillName").getComponent(cc.Label).string = obj.name; //设置名字
            child.getChildByName("skillDesc").getComponent(cc.Label).string = obj.desc; //设置描述
            child.skill = skill; //将技能id存入节点中
        }
    },

    // update (dt) {},
});
